/**
 * 项目名称: Topic_449.cpp
 * 项目路径: study/DataStructure/LeetCode/树/中等
 * 编辑时间: 2024年08月28日08时26分50秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Codec {
public:
    string ans;
    void pre(TreeNode* root){
        if(!root)return;
        ans+=to_string(root->val)+" ";
        pre(root->left);
        pre(root->right);
    }
    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        if (!root)return "";
        pre(root);
        return ans;
    }



    TreeNode* insert(vector<int>& data,int & u,int minv,int maxv){
        if(u == data.size() || data[u]<minv || data[u] > maxv)return nullptr;
        auto root = new TreeNode(data[u++]);
        root->left = insert(data,u, minv,root->val);
        root->right = insert(data,u,root->val+1,maxv);
        return root;
    }
    // Decodes your encoded data to tree.
    TreeNode* deserialize(string str) {
        vector<int>data;
        stringstream ssin(str);
        int x,u=0;
        while (ssin >> x)data.push_back(x);
        return insert(data,u,INT16_MIN,INT16_MAX);
    }
};
